package wsi.exp.calibrate;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import org.rygh.core.html.Html;
import org.rygh.core.html.element.HtmlDiv;
import org.rygh.core.html.element.HtmlPre;
import org.rygh.core.util.UIo;
import org.rygh.core.util.counter.SimpleCounter;
import org.rygh.core.xml.XmlPath;
import org.rygh.semantic.model.ex.PredicatePath;

import wsi.exp.exp3.GoldSetMatch;
import wsi.exp.exp3.PathMatchPair;
import wsi.exp.exp3.compare.Exp3PairComparator;
import wsi.function.DirManager;
import wsi.resolve.FactItem;
import wsi.resolve.ResolveController;

@Deprecated
public class Calibrator extends ResolveController {

	private final List<Exp3PairComparator> results = new ArrayList<>();
	private final GoldSetMatch goldSet = new GoldSetMatch();

	public Calibrator(DirManager dir) {
		super(dir);
	}

	@Override
	public void compute() {
		super.compute();

		// File gsfile = new File(dir.getGoldSetDir(),
		// "gold_set_relations.txt");
		File gsfile = new File("gold_set_relations.txt");

		if (!gsfile.exists()) {
			log.info("No goldset file.");
			return;
		}

		String content = UIo.readFile(gsfile);
		goldSet.load(content);

		// only count relations with at least one match.

		Exp3PairComparator result;

		for (int t = 90; t >= 0; t -= 10) {
			result = new Exp3PairComparator(null, t);
			result.setGoldSet(goldSet);
			result.calibrate(resolver.getFactItemCounter());

			results.add(result);
		}

	}

	private SimpleCounter<String> highlightUnmatched(
			SimpleCounter<String> counter) {
		SimpleCounter<String> ncounter = new SimpleCounter<>();
		ncounter.setNormalizeFactor(counter.getNormalizeFactor());

		PredicatePath path;
		XmlPath xpath;
		PathMatchPair pair, hit;
		String new_key;

		for (Entry<String, Double> entry : counter.getNormalizedMap()
				.entrySet()) {
			String key = entry.getKey();
			FactItem item = (FactItem) counter.getValue(key);

			xpath = item.getXmlLeaf().getXpath();
			path = item.getCenterToValulePath();
			pair = new PathMatchPair(path, xpath);
			hit = goldSet.find(pair);

			if (hit == null)
				new_key = String.format("<span style=\"color:red;\">%s</span>",
						key);
			else
				new_key = key;

			ncounter.set(new_key, counter.get(key));
		}

		return ncounter;
	}

	@Override
	public String save() {
		// UIo.saveFile(new File(dir.getGoldSetDir(), "results.txt"),
		// toString());
		//
		// UIo.saveFile(new File(dir.getGoldSetDir(), "unmatched.html"),
		// toMatchResultString());

		return "";
	}

	/**
	 * highlight unmatched pairs in red.
	 * 
	 * @return
	 */
	public String toMatchResultString() {

		SimpleCounter<String> subTable = highlightUnmatched(resolver
				.getFactItemCounter());

		subTable.setNormalizeFactor(subtreeCounter.getSubMatchFrequency()
				.getNormalizeFactor());

		Html html = new Html(dir.getServiceName() + "/" + function.getName());

		HtmlPre pre = new HtmlPre(goldSet.getContent());
		html.addElement(pre);
		html.newline();

		subTable.setName("Match Info for Subtrees");
		HtmlDiv div = new HtmlDiv(subTable.toHtmlTable());
		html.addElement(div);
		html.newline();

		return html.toString();
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();

		sb.append(String.format("Gold %d\r\n", goldSet.size()));

		for (Exp3PairComparator i : results) {
			sb.append(i.toString());
			sb.append("\r\n");
		}

		return sb.toString();
	}
}
